package com.jiuyun.auth.servlet;

import com.jiuyun.auth.dao.UserDao;
import com.jiuyun.auth.dao.impl.UserDaoImpl;
import com.jiuyun.auth.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    UserDao userDao = new UserDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("uname");
        String pass = request.getParameter("upass");
        System.out.println(name+"---"+pass);
        User user = null;
        try {
           user = userDao.login(name,pass);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        if (user!=null){
            //登录成功 用户放入sessio
            HttpSession session =request.getSession();
            session.setAttribute("LOGIN",user);
            response.sendRedirect("/auth/index.jsp");
        }else {
            request.setAttribute("msg","用户名或密码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
    }
    }

    protected void  doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
